package ces.lawsuit.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import com.jfinal.kit.StrKit;

import ces.lawsuit.model.QueryModel;

public class QueryUtil {
	public static String SPACE=" ";
	public static Map getQueryModel(HttpServletRequest request){
		//Map<String, String[]> parasMap = request.getParameterMap();
		Map queryMap=new  HashMap();
		Map<String, String[]> parasMap = request.getParameterMap();
		for (Entry<String, String[]> e : parasMap.entrySet()) {
			String paraKey = e.getKey();
			if (paraKey.startsWith("queryModel.")) {
				String[] paraValue = e.getValue();
				if(StrKit.notNull(paraValue)&&StrKit.notNull(paraValue[0])){
				queryMap.put(paraKey, paraValue[0]);}
			}
		}
		return queryMap;
	}
	public static String createQuerySql(HttpServletRequest request){
		
		return createQuerySql(getQueryModel(request));
		
	}
	public static QueryModel createSql(HttpServletRequest request){
		StringBuffer sqlBf=new StringBuffer("1=1").append(SPACE);
		List<String> valueList=new ArrayList();
		Map<String, String[]> parasMap = request.getParameterMap();
		for(String key :parasMap.keySet()){
			System.out.println("key: "+key);
			if(key.endsWith(".operator")){
				String limit=parasMap.get(key)[0];
				String queryKey=key.substring(0,key.indexOf(".operator"));
			    sqlBf.append(" and (").append(queryKey).append(SPACE).append(limit).append(SPACE);
			    	sqlBf.append("?)");
			  //  valueList.add(request.getParameter(queryKey));
		        valueList.add(parasMap.get(queryKey)[0]);
			}
		}
		QueryModel t=new QueryModel();
		t.setQuerySql(sqlBf.toString());
		t.setValueList(valueList);
		return t;
		
	}
	public static String createQuerySql(Map map,Object... alias){
		StringBuffer querySql=new StringBuffer("1=1 ");
		if (StrKit.notNull(map)) {
			Set<Map.Entry<String, String>> set = map.entrySet();
			for (Map.Entry<String, String> entry : set) {
				String key = entry.getKey();
				String queryField = key.indexOf(".")>-1?(key.toString()).split("\\.")[1]:key;
				if(queryField.indexOf("_")>-1){
				 queryField=queryField.split("_")[0];
				}
				String queryType = key.indexOf("_")>-1?key.split("_")[1]:App.QueryType.LIKE;
				String queryWord = entry.getValue();
			//	String fieldType = App.QueryType.;
				  
//				if(Util.isIn(queryWord.trim(),new String[]{"%","\\","'"})){
//					continue;
//				}
//				if(queryField.indexOf("owner")>-1 && Util.notNull(alias) && alias.length>0){
//					queryField = queryField + "." + alias[0];
//					
//				   fieldType=Jcpt.JavaDataType.LONG;
//					
//					
//				}else{
//					fieldType = Util.getFieldType(this.getBean().getClass(),queryField);
//				}
				
//				if(queryField.startsWith("start")){
//					queryField = queryField.substring(5);
//					queryField = queryField.substring(0, 1).toLowerCase()+queryField.substring(1);
//				}else if(queryField.startsWith("end")){
//					queryField = queryField.substring(3);
//					queryField = queryField.substring(0, 1).toLowerCase()+queryField.substring(1);
//				}				

				if (App.QueryType.LIKE.equals(queryType)) {
					querySql.append(" and "+queryField+" "+ App.QueryType.LIKE+" '%" + queryWord.trim() + "%'" );
				} else if (App.QueryType.EQ.equals(queryType)) {
					querySql.append(" and "+queryField+" ='"+queryWord.trim()+"'");
				} else if (App.QueryType.GE.equals(queryType)) {
					querySql.append("  and "+queryField+" >='"+queryWord.trim()+"'");
				} else if (App.QueryType.GT.equals(queryType)) {
					querySql.append(" and "+queryField+" >'"+queryWord.trim()+"'");
				} else if (App.QueryType.LE.equals(queryType)) {
					querySql.append(" and "+queryField+" <='"+queryWord.trim()+"'");
				} else if (App.QueryType.LT.equals(queryType)) {
					querySql.append(" and "+queryField+" <'"+queryWord.trim()+"'");
				} else if (App.QueryType.IN.equals(queryType)) {
					querySql.append("  and "+queryField+" in('"+queryWord.trim()+"')");
				}
			}
		}
		return querySql.toString();
	}
}
